package org.ohos.rajawali3d.examples.examples.materials;

import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.TextureHolder;
import ohos.animation.AccelerateDecelerateInterpolator;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.media.player.Player;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation;
import org.rajawali3d.animation.EllipticalOrbitAnimation3D;
import org.rajawali3d.animation.TranslateAnimation3D;
import org.rajawali3d.lights.PointLight;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.methods.DiffuseMethod;
import org.rajawali3d.materials.methods.SpecularMethod;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.materials.textures.StreamingTexture;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Cube;
import org.rajawali3d.primitives.Plane;

import java.io.IOException;

public class VideoTextureFragment extends AExampleFragment {

    private SurfaceProvider fragmentFrame;


    @Override
    public AExampleRenderer createRenderer() {
        return new VideoTextureRenderer(getContext(), this);
    }

    private final class VideoTextureRenderer extends AExampleRenderer {
        private Player mMediaPlayer;
        private StreamingTexture mVideoTexture;

        public VideoTextureRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            PointLight pointLight = new PointLight();
            pointLight.setPower(1);
            pointLight.setPosition(-1, 1, 4);

            getCurrentScene().addLight(pointLight);
            getCurrentScene().setBackgroundColor(0xff040404);
            {
                Object3D ohos = new Cube(2.0f);
                Material material = new Material();
                material.enableLighting(true);
                material.setDiffuseMethod(new DiffuseMethod.Lambert());
                material.setSpecularMethod(new SpecularMethod.Phong());
                ohos.setMaterial(material);
                ohos.setColor(0xff99C224);
                //getCurrentScene().addChild(ohos);
            }
            mMediaPlayer = new Player(getContext());
            ResourceManager resManager = mContext.getResourceManager();
            RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/sintel_trailer_480p.mp4");
            try {
                mMediaPlayer.setSource(rawFileEntry.openRawFileDescriptor());
            } catch (IOException e) {
                e.printStackTrace();
            }
            mMediaPlayer.enableSingleLooping(true);

            mVideoTexture = new StreamingTexture("sintelTrailer", mMediaPlayer);
            Material material = new Material();
            material.setColorInfluence(0);
            try {
                material.addTexture(mVideoTexture);
            } catch (ATexture.TextureException e) {
                e.printStackTrace();
            }

            Plane screen = new Plane(3, 2, 2, 2, Vector3.Axis.Z);
            screen.setMaterial(material);
            screen.setX(.1f);
            screen.setY(-.2f);
            screen.setZ(1.5f);
            getCurrentScene().addChild(screen);

            getCurrentCamera().enableLookAt();
            getCurrentCamera().setLookAt(0, 0, 0);

            // -- animate the spot light

            TranslateAnimation3D lightAnim = new TranslateAnimation3D(
                    new Vector3(-3, 3, 10), // from
                    new Vector3(3, 1, 3)); // to
            lightAnim.setDurationMilliseconds(5000);
            lightAnim.setRepeatMode(Animation.RepeatMode.REVERSE_INFINITE);
            lightAnim.setTransformable3D(pointLight);
            lightAnim.setInterpolator(new AccelerateDecelerateInterpolator());
            getCurrentScene().registerAnimation(lightAnim);
            lightAnim.play();

            // -- animate the camera

            EllipticalOrbitAnimation3D camAnim = new EllipticalOrbitAnimation3D(
                    new Vector3(3, 2, 10), new Vector3(1, 0, 8), 0, 359);
            camAnim.setDurationMilliseconds(20000);
            camAnim.setRepeatMode(Animation.RepeatMode.INFINITE);
            camAnim.setTransformable3D(getCurrentCamera());
            getCurrentScene().registerAnimation(camAnim);
            camAnim.play();

            //mMediaPlayer.setVideoSurface(fragmentFrame.getSurfaceOps().get().getSurface());
            mMediaPlayer.prepare();
            mMediaPlayer.play();
        }

        @Override
        protected void onRender(long elapsedRealtime, double deltaTime) {
            super.onRender(elapsedRealtime, deltaTime);
            mVideoTexture.update();
        }

        @Override
        public void onPause() {
            super.onPause();
            if (mMediaPlayer != null)
                mMediaPlayer.pause();
        }

        @Override
        public void onResume() {
            super.onResume();
            if (mMediaPlayer != null)
                mMediaPlayer.play();
        }

        @Override
        public void onRenderSurfaceDestroyed(TextureHolder surfaceTexture) {
            super.onRenderSurfaceDestroyed(surfaceTexture);
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }

    }

}
